import { withRouter } from 'next/router';
import { destination_map } from '@/config';
// 组件
import MainBody from '@/pages_components/destination/Main';
import _404 from '@/pages/404';
// ssr api
import { destinationDetail_SSR, destinationPart_SSR } from '@/ssr';

const Destination = ({ router, ssr, ssr_route, ssr_hotel, ssr_ticket }) => {
    const { destination } = router.query;
    // 未找到产品
    if(!ssr){
        return <_404 />
    }
    const { id, city, url } = destination_map[destination];
    return (
        <MainBody
            params={[{ name: city, url }]}
            id={id}
            city={city}
            data={{ssr, ssr_route, ssr_hotel, ssr_ticket}}
        />
    )
}

// ssr 渲染
Destination.getInitialProps = async (res) => {
    const { destination } = res.query;
    if(destination_map[destination]){
        const { id } = destination_map[destination],
              { ssr } = await destinationDetail_SSR(id),
              { 
                ssr: { ssr_route, ssr_hotel, ssr_ticket }
              } = await destinationPart_SSR(destination_map[destination], {t: 4, h: 3, r: 3}, false);
              
        return { ssr, ssr_route, ssr_hotel, ssr_ticket }
    }else{
        return { ssr: null }
    }
}

export default withRouter(Destination)